﻿// https://leetcode-cn.com/problems/path-sum-ii/
// 113. 路径总和 II

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <limits.h>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<vector<int>> result;
    vector<int> vec;

    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        
        if (root == nullptr) return result;
        vec.push_back(root->val);
        FindPath(root, targetSum - root->val);
        return result;
    }

    void FindPath(TreeNode *root, int count) {
        if (!root->left && !root->right && count == 0) {
            result.push_back(vec);
            return;
        }
        if (!root->left && !root->right) {
            return;
        }

        if (root->left) {
            vec.push_back(root->left->val);
            FindPath(root->left, count - root->left->val);
            vec.pop_back();
        }
        if (root->right) {
            vec.push_back(root->right->val);
            FindPath(root->right, count - root->right->val);
            vec.pop_back();
        }
        return;
    }
};

int main() {
    Solution obj = Solution();
}